{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f84d496-a970-452d-99a1-2f67718dff9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from datasets import DatasetDict, Dataset\n",
    "from PIL import Image\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a8ba1f2-d465-4b78-a48a-744f591a14ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "turn your json to DatasetDict\n",
    "\"\"\"\n",
    "def json_to_dataset(json_file_path):\n",
    "    # read json file\n",
    "    with open(json_file_path, 'r') as f:\n",
    "        data = json.load(f)\n",
    "\n",
    "    image_paths = [item['image_path'] for item in data]\n",
    "    problems = [item['problem'] for item in data]\n",
    "    solutions = [item['solution'] for item in data]\n",
    "\n",
    "    images = [Image.open(image_path).convert('RGBA') for image_path in image_paths]\n",
    "\n",
    "    dataset_dict = {\n",
    "        'image': images,\n",
    "        'problem': problems,\n",
    "        'solution': solutions\n",
    "    }\n",
    "\n",
    "    dataset = Dataset.from_dict(dataset_dict)\n",
    "    dataset_dict = DatasetDict({\n",
    "        'train': dataset\n",
    "    })\n",
    "    return dataset_dict\n",
    "\n",
    "\n",
    "time1 = time.asctime()\n",
    "print(time1)\n",
    "### Your dataset in JSON file format consists of three parts: image, problem and solution\n",
    "dataset_dict = json_to_dataset('your_dataset_json_file.json')\n",
    "time2 = time.asctime()\n",
    "print(time2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e2a20b4-131f-49fa-baee-4c2675479de3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "save to your local disk\n",
    "\"\"\"\n",
    "def save_dataset(dataset_dict, save_path):\n",
    "    # save DatasetDict to your disk\n",
    "    dataset_dict.save_to_disk(save_path)\n",
    "\n",
    "save_path = './share_data/your_local_dataset'\n",
    "save_dataset(dataset_dict, save_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22aa1938-3b68-4df3-896d-74c8b7c854c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "read from your local disk\n",
    "\"\"\"\n",
    "def load_dataset(save_path):\n",
    "    # load DatasetDict\n",
    "    return DatasetDict.load_from_disk(save_path)\n",
    "\n",
    "test_dataset_dict = load_dataset('./share_data/your_local_dataset')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
